﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using CustomerMgr.Data;
using CustomerMgr.Data.Models;
namespace CustomerMgr.Web
{
    public partial class Order : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                RadComboBox1.DataSource = CustomerFactory.SelectAll();
            }
        }
        protected void RadComboBox1_ItemDataBound(object sender, RadComboBoxItemEventArgs e)
        {
            e.Item.Text = string.Concat(e.Item.Text.ToLower().Split(' ')[0], "@telerik.com");
        }
        protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            RadGrid1.DataSource = OrderDetailFactory.SelectAll();
        }
        protected void RadGrid1_UpdateCommand(object source, GridCommandEventArgs e)
        {
            GridEditableItem editedItem = e.Item as GridEditableItem;
            int cusId = (int)editedItem.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Id"];
            OrderDetail cus = OrderDetailFactory.SelectOne(cusId);

            TextBox txtProductName = (TextBox)editedItem.FindControl("txtProductName");
            RadNumericTextBox txtPrice = (RadNumericTextBox)editedItem.FindControl("tbUnitPrice");
            RadNumericTextBox txtQuantity = (RadNumericTextBox)editedItem.FindControl("txtQuantity");

            cus.ProductName = txtProductName.Text;
            cus.Price = Convert.ToDecimal(txtPrice.Text.Trim());
            cus.Quantity = Convert.ToInt16(txtQuantity.Text.Trim());

            if (OrderDetailFactory.Update(cus))
            {
                RadGrid1.Rebind();
            }

        }
        protected void RadGrid1_InsertCommand(object source, GridCommandEventArgs e)
        {
            GridEditableItem editedItem = e.Item as GridEditableItem;
            CustomerMgr.Data.Models.OrderDetail cus = new Data.Models.OrderDetail();

            TextBox txtProductName = (TextBox)editedItem.FindControl("txtProductName");
            RadNumericTextBox txtPrice = (RadNumericTextBox)editedItem.FindControl("tbUnitPrice");
            RadNumericTextBox txtQuantity = (RadNumericTextBox)editedItem.FindControl("txtQuantity");

            cus.ProductName = txtProductName.Text;
            cus.Price = Convert.ToDecimal(txtPrice.Text.Trim());
            cus.Quantity = Convert.ToInt16(txtQuantity.Text.Trim());

            if (hdfCustomerId.Value == "0")
            {
                Data.Models.Customer customernew = new Data.Models.Customer();
                customernew.Name = txtAddCusName.Text.Trim();
                if (CustomerFactory.Insert(customernew) != null)
                {
                    hdfCustomerId.Value = customernew.Id.ToString();                    
                }
            }
            if (hdfOrderId.Value == "0")
            {
                Data.Models.Order entity = new Data.Models.Order();
                entity.CustomerId = Convert.ToInt32(hdfCustomerId.Value);
                entity.CreateDate = RadDatePicker1.SelectedDate.Value;
                if (OrderDetailFactory.InsertOrder(entity) != null)
                {
                    hdfOrderId.Value = entity.Id.ToString();                    
                }
            }
            cus.OrderId = Convert.ToInt32(hdfOrderId.Value);
            if (OrderDetailFactory.Insert(cus))
            {
                RadGrid1.Rebind();
            }
        }

        protected void RadGrid1_DeleteCommand(object source, GridCommandEventArgs e)
        {
            int cusId = (int)(e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Id"];
            OrderDetail cus = OrderDetailFactory.SelectOne(cusId);
            if (OrderDetailFactory.Delete(cus))
            {
                RadGrid1.Rebind();
            }
        }

        protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
        {
            if (e.Item is GridDataItem)
            {
                Label lbl = e.Item.FindControl("lblOrder") as Label;
                lbl.Text = (e.Item.ItemIndex + 1).ToString();
            }
        }

        protected void btnAddCustomer_Click(object sender, EventArgs e)
        {

        }
    }
}